package com.card.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import org.apache.commons.lang3.StringUtils;

/**
 * ClassName: ValidUtil <br/>
 * date: 2018年9月5日 下午6:00:11 <br/>
 * 
 * @author lishuai11
 * @version
 * @since JDK 1.8
 */
public class ValidUtil {

	private static ValidatorFactory factory = Validation.buildDefaultValidatorFactory();

	public static <T> List<String> validate(T t) {
		Validator validator = factory.getValidator();
		Set<ConstraintViolation<T>> constraintViolations = validator.validate(t);
		List<String> messageList = new ArrayList<>();
		for (ConstraintViolation<T> constraintViolation : constraintViolations) {
			messageList.add(constraintViolation.getMessage());
		}
		return messageList;
	}

	/**
	 * 
	 * strIsNotNullOrEmpty:(n个字符串都不是空). <br/>
	 * 
	 * @author lishuai11
	 * @param params
	 * @return
	 * @since JDK 1.8
	 */
	public static boolean strIsNotNullOrEmpty(String... params) {
		for (String param : params) {
			if (StringUtils.isBlank(param)) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 
	 * phoneIsValid:(电话号). <br/>
	 * 
	 * @author lishuai11
	 * @param phone
	 * @return
	 * @since JDK 1.8
	 */
	public static boolean mobileIsValid(String mobile) {
		String check = "^(((13[0-9])|(14[579])|(15([0-3]|[5-9]))|(16[6])|(17[0135678])|(18[0-9])|(19[89]))\\d{8})$";
		Pattern regex = Pattern.compile(check);
		Matcher matcher = regex.matcher(mobile);
		return matcher.matches();
	}

	/**
	 * 
	 * codeIsValid:(邀请码是否合法). <br/>
	 * 
	 * @author lishuai11
	 * @param inviteCode
	 * @return
	 * @since JDK 1.8
	 */
	public static boolean codeIsValid(String inviteCode) {
		String check = "^\\d{6}$";
		Pattern regex = Pattern.compile(check);
		Matcher matcher = regex.matcher(inviteCode);
		return matcher.matches();
	}

}
